﻿

#include "zyComboBoxEx.h"


DarkMoonUI::zyComboBoxEx::zyComboBoxEx()
{
	SetMember(L"this", (LONG_PTR)this);
	m_obj->setWndProc(zyComboBoxEx_WndProc);
	m_obj->setChildWndProc(zyComboBoxEx_ChildWndProc);
	m_obj->setDestroy(zyComboBoxEx_Destroy);
}
DarkMoonUI::zyComboBoxEx::~zyComboBoxEx()
{

}
void DarkMoonUI::zyComboBoxEx::OnNCDestroy()
{
	zyComboBoxEx* pComboBox = (zyComboBoxEx*)GetMember(L"pComboBox");
	if (pComboBox != nullptr) {
		pComboBox->getObject()->release();
		delete pComboBox;
		SetMember(L"pComboBox", 0);
	}
	zyEditBox* pEdit = (zyEditBox*)GetMember(L"pEdit");
	if (pEdit != nullptr) {
		pEdit->getObject()->release();
		delete pEdit;
		SetMember(L"pEdit", 0);
	}
	resObject* pList = (resObject*)GetMember(L"pImageList");
	if (pList != nullptr) {
		pList->release();
		//delete pList;
		//ImageList_Destroy(pList->取句柄());
		SetMember(L"pImageList", 0);
	}
}
void WINAPI DarkMoonUI::zyComboBoxEx::zyComboBoxEx_Destroy(zyObject* obj)
{
	zyComboBoxEx* zyb = (zyComboBoxEx*)obj->getPtrData(L"this");
	zyb->OnNCDestroy();
	zyCommandCtrol::zyCommand_Destroy(obj);
}
LRESULT WINAPI DarkMoonUI::zyComboBoxEx::zyComboBoxEx_ChildWndProc(zyObject* obj, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, BOOL* bHandle)
{
	if (message == WM_COMMAND)
	{
		short id = LOWORD(wParam);// & 65535); //LOWORD
		int code = HIWORD(wParam);
		if (code == CBN_SELCHANGE) {
			TRegEvent* evtPtr = (TRegEvent*)obj->getPtrData(L"OnSelChange");
			if (evtPtr != nullptr) {
				evtPtr->handler<LRESULT>(lParam ,id);
			}
		}
		else if (code == CBN_EDITCHANGE) {
			TRegEvent* evtPtr = (TRegEvent*)obj->getPtrData(L"OnEditChange");
			if (evtPtr != nullptr)
				evtPtr->handler<LRESULT>(lParam, id);
		}
		else if (code == CBN_DROPDOWN) {
			TRegEvent* evtPtr = (TRegEvent*)obj->getPtrData(L"OnDropDown");
			if (evtPtr != nullptr)
				evtPtr->handler<LRESULT>(lParam, id);
		}
		else if (code == CBN_CLOSEUP) {
			TRegEvent* evtPtr = (TRegEvent*)obj->getPtrData(L"OnCloseUp");
			if (evtPtr != nullptr)
				evtPtr->handler<LRESULT>(lParam, id);
		}
		else if (code == CBN_DBLCLK) {
			TRegEvent* evtPtr = (TRegEvent*)obj->getPtrData(L"OnDBClick");
			if (evtPtr != nullptr)
				evtPtr->handler<LRESULT>(lParam, id);
		}
	}
	return zyCommandCtrol::zyCommand_ChildWndProc(obj, hWnd, message, wParam, lParam, bHandle);
}
LRESULT WINAPI DarkMoonUI::zyComboBoxEx::zyComboBoxEx_WndProc(zyObject* obj, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL* bHandled)
{
	if (uMsg == WM_NCDESTROY) {
		zyComboBoxEx* zyb = (zyComboBoxEx*)obj->getPtrData(L"this");
		zyb->OnNCDestroy();
	}
	return zyCommandCtrol::zyCommand_WndProc(obj, hWnd, uMsg, wParam, lParam, bHandled);
}

BOOL DarkMoonUI::zyComboBoxEx::创建(HWND 参数_父窗口, const std::wstring& 参数_标题, POINT 参数_位置, POINT 参数_尺寸, int 参数_ID, int 参数_窗口风格, int 参数_扩展风格)
{
	if (参数_窗口风格 == 0)
		参数_窗口风格 = CBS_DROPDOWN | WS_TABSTOP | CBS_AUTOHSCROLL | WS_VSCROLL;
	if (参数_ID == NULL)
		参数_ID = 黑月默认ID();

	return _CreateCtrol(参数_父窗口, 参数_标题, 参数_位置, 参数_尺寸, 参数_ID, 参数_窗口风格 | CBS_NOINTEGRALHEIGHT , 参数_扩展风格, L"ComboBoxEx32");
}

BOOL DarkMoonUI::zyComboBoxEx::创建(zyWindow& 参数_父窗口, const std::wstring& 参数_标题, POINT 参数_位置, POINT 参数_尺寸, int 参数_ID, int 参数_窗口风格, int 参数_扩展风格)
{
	if (参数_窗口风格 == 0)
		参数_窗口风格 = CBS_DROPDOWN | WS_TABSTOP | CBS_AUTOHSCROLL | WS_VSCROLL;
	if (参数_ID == NULL)
		参数_ID = 黑月默认ID();

	return _CreateCtrol(参数_父窗口.getHwnd(), 参数_标题, 参数_位置, 参数_尺寸, 参数_ID, 参数_窗口风格 | CBS_NOINTEGRALHEIGHT , 参数_扩展风格, L"ComboBoxEx32");
}
int DarkMoonUI::zyComboBoxEx::插入项目(int 参数_插入索引, const std::wstring& 参数_项目标题, int 参数_图片索引, int 参数_选中图片索引, int 参数_覆盖图片索引, int 参数_缩进距离, int 参数_项目数值) {
	COMBOBOXEXITEM cbex = { 0 };
	cbex.mask = CBEIF_TEXT | CBEIF_LPARAM | CBEIF_DI_SETITEM;
	if (参数_图片索引 != -1) {
		cbex.mask = cbex.mask | CBEIF_IMAGE | CBEIF_SELECTEDIMAGE;
		cbex.iImage = 参数_图片索引;
		cbex.iSelectedImage = 参数_图片索引;
	}
	if (参数_选中图片索引 != -1) {
		cbex.mask = cbex.mask | CBEIF_SELECTEDIMAGE;
		cbex.iSelectedImage = 参数_选中图片索引;
	}
	if (参数_覆盖图片索引 != -1) {
		cbex.mask = cbex.mask | CBEIF_OVERLAY;
		cbex.iOverlay = 参数_覆盖图片索引 + 1;
	}
	if (参数_缩进距离 != -1) {
		cbex.mask = cbex.mask | CBEIF_INDENT;
		cbex.iIndent = 参数_缩进距离;
	}
	cbex.iItem = 参数_插入索引;
	cbex.pszText = (LPWSTR)参数_项目标题.c_str();
	cbex.cchTextMax = (int)参数_项目标题.size();
	cbex.lParam = 参数_项目数值;
	return (int)sendMsg(CBEM_INSERTITEMW, 0, (LPARAM)&cbex);
}
bool DarkMoonUI::zyComboBoxEx::置现行选中项(int 参数_项目索引) {
	int i = (int)sendMsg(CB_SETCURSEL, 参数_项目索引, 0);
	if (i != CB_ERR)
		return true;
	return false;
}
int DarkMoonUI::zyComboBoxEx::取现行选中项() {
	return (int)sendMsg(CB_GETCURSEL, 0, 0);
}
void DarkMoonUI::zyComboBoxEx::置图片组(zyLmageList& 参数_图片组) {
	resObject* pImageList = reinterpret_cast<resObject*>(GetMember(L"pImageList"));
	if (pImageList != nullptr)
		pImageList->release();
	pImageList = 参数_图片组.getObject();
	pImageList->addRef();
	SetMember(L"pImageList", (LONG_PTR)pImageList);
	sendMsg(CBEM_SETIMAGELIST, 0, (LPARAM)参数_图片组.取句柄());
}
DarkMoonUI::zyLmageList DarkMoonUI::zyComboBoxEx::取图片组() {
	resObject* pImageList = (resObject*)GetMember(L"pImageList");
	if (pImageList != nullptr) {
		zyLmageList imagelist;
		imagelist.setObject(pImageList);
		return imagelist;
	}
	zyLmageList newimagelist;
	HIMAGELIST himl = (HIMAGELIST)sendMsg(CBEM_GETIMAGELIST, 0, 0);
	newimagelist.置句柄(himl);
	resObject* res = newimagelist.getObject();
	res->addRef();
	//IUnknown_AddRef (pImageList)
	SetMember(L"pImageList", (LONG_PTR)res);
	return newimagelist;
}
std::wstring DarkMoonUI::zyComboBoxEx::取项目文本(int 参数_项目索引) {
	std::wstring pszText;
	COMBOBOXEXITEM cbex = { 0 };
	pszText.reserve(520);
	cbex.mask = CBEIF_TEXT;
	cbex.iItem = 参数_项目索引;
	cbex.pszText = (LPWSTR)pszText.c_str();
	cbex.cchTextMax = (int)pszText.size();
	if (sendMsg(CBEM_GETITEMW, 0, (LPARAM)&cbex) != 0) {
		return pszText;
	}
	return L"";
}
bool DarkMoonUI::zyComboBoxEx::置项目文本(int 参数_项目索引, const std::wstring& 参数_项目文本) {
	COMBOBOXEXITEM cbex = { 0 };
	cbex.mask = CBEIF_TEXT;
	cbex.iItem = 参数_项目索引;
	cbex.pszText = (LPWSTR)参数_项目文本.c_str();
	cbex.cchTextMax = (int)参数_项目文本.size();
	return sendMsg(CBEM_SETITEMW, 0, (LPARAM)&cbex) != 0;
}
int DarkMoonUI::zyComboBoxEx::取图片(int 参数_项目索引) {
	COMBOBOXEXITEM cbex = { 0 };
	cbex.mask = CBEIF_IMAGE;
	cbex.iItem = 参数_项目索引;
	if (sendMsg(CBEM_GETITEMW, 0, (LPARAM)&cbex) == 0) {
		return -1;
	}
	return cbex.iImage;
}
bool DarkMoonUI::zyComboBoxEx::置图片(int 参数_项目索引, int 参数_图片索引) {
	COMBOBOXEXITEM cbex = { 0 };
	cbex.mask = CBEIF_IMAGE;
	cbex.iItem = 参数_项目索引;
	cbex.iImage = 参数_图片索引;
	return sendMsg(CBEM_SETITEMW, 0, (LPARAM)&cbex) != 0;
}
int DarkMoonUI::zyComboBoxEx::取选中图片(int 参数_项目索引) {
	COMBOBOXEXITEM cbex = { 0 };
	cbex.mask = CBEIF_SELECTEDIMAGE;
	cbex.iItem = 参数_项目索引;
	if (sendMsg(CBEM_GETITEMW, 0, (LPARAM)&cbex) == 0) {
		return -1;
	}
	return cbex.iSelectedImage;
}
bool DarkMoonUI::zyComboBoxEx::置选中图片(int 参数_项目索引, int 参数_选中图片索引) {
	COMBOBOXEXITEM cbex = { 0 };
	cbex.mask = CBEIF_SELECTEDIMAGE;
	cbex.iItem = 参数_项目索引;
	cbex.iSelectedImage = 参数_选中图片索引;
	return sendMsg(CBEM_SETITEMW, 0, (LPARAM)&cbex) != 0;
}
int DarkMoonUI::zyComboBoxEx::取覆盖图片(int 参数_项目索引) {
	COMBOBOXEXITEM cbex = { 0 };
	cbex.mask = CBEIF_OVERLAY;
	cbex.iItem = 参数_项目索引;
	if (sendMsg(CBEM_GETITEMW, 0, (LPARAM)&cbex) == 0) {
		return -1;
	}
	return cbex.iOverlay - 1;
}
bool DarkMoonUI::zyComboBoxEx::置覆盖图片(int 参数_项目索引, int 参数_图片索引) {
	COMBOBOXEXITEM cbex = { 0 };
	cbex.mask = CBEIF_OVERLAY;
	cbex.iItem = 参数_项目索引;
	cbex.iOverlay = 参数_图片索引 + 1;
	return sendMsg(CBEM_SETITEMW, 0, (LPARAM)&cbex) != 0;
}
INT_PTR DarkMoonUI::zyComboBoxEx::取项目数值(int 参数_项目索引) {
	COMBOBOXEXITEM cbex = { 0 };
	cbex.mask = CBEIF_LPARAM;
	cbex.iItem = 参数_项目索引;
	sendMsg(CBEM_GETITEMW, 0, (LPARAM)&cbex);
	return cbex.lParam;
}
bool DarkMoonUI::zyComboBoxEx::置项目数值(int 参数_项目索引, INT_PTR 参数_项目数值) {
	COMBOBOXEXITEM cbex = { 0 };
	cbex.mask = CBEIF_LPARAM;
	cbex.iItem = 参数_项目索引;
	cbex.lParam = 参数_项目数值;
	return sendMsg(CBEM_SETITEMW, 0, (LPARAM)&cbex) != 0;
}
int DarkMoonUI::zyComboBoxEx::取项目缩进(int 参数_项目索引) {
	COMBOBOXEXITEM cbex = { 0 };
	cbex.mask = CBEIF_INDENT;
	cbex.iItem = 参数_项目索引;
	sendMsg(CBEM_GETITEMW, 0, (LPARAM)&cbex);
	return cbex.iIndent;
}
bool DarkMoonUI::zyComboBoxEx::置项目缩进(int 参数_项目索引, int 参数_项目缩进) {
	COMBOBOXEXITEM cbex = { 0 };
	cbex.mask = CBEIF_INDENT;
	cbex.iItem = 参数_项目索引;
	cbex.iIndent = 参数_项目缩进;
	return sendMsg(CBEM_SETITEMW, 0, (LPARAM)&cbex) != 0;
}
DarkMoonUI::zyComboBoxEx* DarkMoonUI::zyComboBoxEx::取组合框() {//当组合框风格为"组合框_允许编辑"时，获取其中的编辑框控件
	zyComboBoxEx* pComboBox = (zyComboBoxEx*)GetMember(L"pComboBox");
	if (pComboBox != nullptr) {
		return pComboBox;
	}
	HWND hwndItem = (HWND)SendMessage(取窗口句柄(), CBEM_GETCOMBOCONTROL, 0, 0);
	if (IsWindow(hwndItem) != 0) {
		pComboBox = new zyComboBoxEx();
		pComboBox->_BindWindow(hwndItem);
		pComboBox->getObject()->addRef();
		//IUnknown_AddRef (pComboBox)
		SetMember(L"pComboBox", (LONG_PTR)pComboBox);
	}
	return pComboBox;
}
DarkMoonUI::zyEditBox* DarkMoonUI::zyComboBoxEx::取编辑框() {//当组合框风格为"组合框_允许编辑"时，获取其中的编辑框控件
	zyEditBox* pEdit = (zyEditBox*)GetMember(L"pEdit");
	if (pEdit != nullptr) {
		return pEdit;
	}
	HWND hwndItem = (HWND)SendMessage(取窗口句柄(), CBEM_GETEDITCONTROL, 0, 0);
	if (IsWindow(hwndItem) != 0) {
		pEdit = new zyEditBox();
		pEdit->_BindWindow(hwndItem);
		zyObject* obj = pEdit->getObject();
		if (obj)
			obj->addRef();
		//IUnknown_AddRef (pEdit)
		SetMember(L"pEdit", (LONG_PTR)pEdit);
	}
	return pEdit;
}